<?php
namespace app\admin\controller;

use think\Db;
use think\Loader;

class Article extends Base
{
    //..........
    public function lst()
    {
        $article = Db::name('article')->alias('a')->join('ym_cate c', 'c.id = a.cateid', 'LEFT')->field('a.id,a.title,a.keywords,a.desc,a.click,c.catename,a.conter,a.time,a.pic')->order('a.id')->paginate(5);
//        var_dump($article);die;
        $this->assign('faded', $article);
        return $this->fetch();
    }

    /*
     * 添加文章
     * */
    public function add()
    {
        if (request()->isPost()) {   //post提交的数据
            $data = [
                'title' => input('title'),
                'keywords' => input('keywords'),
                'desc' => input('desc'),
                'cateid' => input('cateid'),
                'conter' => input('content'),
                'time' => time(),
            ];
            if ($_FILES['pic']['tmp_name']) { //图片添加
                $file = request()->file('pic');
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static/uploads');
                if ($info) {
                    $data['pic'] = '/static/uploads/' . date('Ymd') . '/' . $info->getFilename();
                } else {
                    echo $this->error($file->getError());
                }
            }
            $validate = Loader::validate('article');//实例化验证器
            if ($validate->check($data)) {//验证数据
                if (Db::name('article')->insert($data)) {
                    return $this->success('添加文章成功', 'lst');
                } else {
                    return $this->success('添加文章失败');
                }
            } else {
                return $this->error($validate->getError());
            }
            return;
        }
        $cates = db('cate')->order('id')->select();
        $this->assign('cates', $cates);
        return $this->fetch();
    }

    /*
     *删除文章
     * */
    public function del()
    {
        $id = input('id');
        if (db('article')->delete($id)) {
            return $this->success('删除栏目成功！', 'lst');
        } else {
            return $this->success('删除栏目失败！');
        }
    }

    /*
     * 修改文章
     * */
    public function edit()
    {
        if (request()->isPost()) {
            $article = [
                'id'=>input('id'),
                'title' => input('title'),
                'keywords' => input('keywords'),
                'desc' => input('desc'),
                'cateid' => input('cateid'),
                'conter' => input('content'),
//                'time' => time(),
            ];
            if ($_FILES['pic']['tmp_name']) {// 确认有没有图片上传
                $file = request()->file('pic');
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static/uploads');
                if ($info) {
                    $article['pic'] = '/static/uploads/' . date('Ymd') . '/' . $info->getFilename();
                } else {
                    return $this->error($file->getError());
                }
            }

            $validate = Loader::validate('article');
            if ($validate->check($article)) {
                if (db('article')->update($article)) {
                    return $this->success('修改文章成功','lst');
                } else {
                    return $this->success('修改文章失败');
                }
            } else {
                return $this->error($validate->getError());
            }
        }
        $id = input('id');
        $editdata = db('article')->where('id', $id)->find();
        $this->assign('editdata', $editdata);
        $cates = db('cate')->order('id')->select();
        $this->assign('cates', $cates);
        return $this->fetch();
    }
}